From 175693a2e1268a3c3654cdca62d0ec00ec2f00f1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 28 Jan 2015 22:28:31 -0800 Subject: [PATCH] Consider all targets for platform requirements Previously a build script would assign itself to the platform requirement for the first target of a package, but it's more correct to consider all targets for platform requirements as there may be both a plugin and target requirement. Closes #1230 --- src/cargo/ops/cargo_rustc/context.rs | 2 +- src/cargo/ops/cargo_rustc/mod.rs | 13 +++++++++---- tests/test_cargo_compile_plugins.rs | 22 ++++++++++++++++++++++ tests/test_cargo_cross_compile.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 81573d8b4..7e4f79cba 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -360,7 +360,7 @@ impl<'a, 'b: 'a> Context<'a, 'b> { } impl Platform { - fn combine(self, other: Platform) -> Platform { + pub fn combine(self, other: Platform) -> Platform { match (self, other) { (Platform::Target, Platform::Target) => Platform::Target, (Platform::Plugin, Platform::Plugin) => Platform::Plugin, diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 20e72393a..90f48e230 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -284,10 +284,13 @@ fn compile<'a, 'b>(targets: &[&'a Target], pkg: &'a Package, // it once per context. if !target.get_profile().is_custom_build() { continue } let mut reqs = Vec::new(); - let requirement = targets.iter().find(|t| { - !t.get_profile().is_custom_build() && !t.get_profile().is_doc() - }).map(|&other_target| { - cx.get_requirement(pkg, other_target) + let requirement = targets.iter().fold(None::, |req, t| { + if !t.get_profile().is_custom_build() && !t.get_profile().is_doc() { + let r2 = cx.get_requirement(pkg, *t); + req.map(|r| r.combine(r2)).or(Some(r2)) + } else { + req + } }).unwrap_or(Platform::Target); match requirement { Platform::Target => reqs.push(Platform::Target), @@ -369,6 +372,7 @@ fn rustc(package: &Package, target: &Target, Ok((Work::new(move |desc_tx| { let mut rustc = rustc; + debug!("about to run: {}", rustc); // Only at runtime have we discovered what the extra -L and -l // arguments are for native libraries, so we process those here. We @@ -412,6 +416,7 @@ fn rustc(package: &Package, target: &Target, pass_l_flag: bool, current_id: &PackageId) -> CommandPrototype { for id in native_lib_deps.into_iter() { + debug!("looking up {} {:?}", id, kind); let output = &build_state[(id.clone(), kind)]; for path in output.library_paths.iter() { rustc = rustc.arg("-L").arg(path); diff --git a/tests/test_cargo_compile_plugins.rs b/tests/test_cargo_compile_plugins.rs index d20714cb7..f2a5d7af1 100644 --- a/tests/test_cargo_compile_plugins.rs +++ b/tests/test_cargo_compile_plugins.rs @@ -165,3 +165,25 @@ test!(plugin_with_dynamic_native_dependency { assert_that(foo.cargo_process("build").env("SRC", Some(lib.as_vec())), execs().with_status(0)); }); + +test!(plugin_integration { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + build = "build.rs" + + [lib] + name = "foo" + plugin = true + doctest = false + "#) + .file("build.rs", "fn main() {}") + .file("src/lib.rs", "") + .file("tests/it_works.rs", ""); + + assert_that(p.cargo_process("test"), + execs().with_status(0)); +}); diff --git a/tests/test_cargo_cross_compile.rs b/tests/test_cargo_cross_compile.rs index a4d2ed024..968ff1aca 100644 --- a/tests/test_cargo_cross_compile.rs +++ b/tests/test_cargo_cross_compile.rs @@ -652,3 +652,30 @@ test!(build_script_only_host { assert_that(p.cargo_process("build").arg("--target").arg(&target).arg("-v"), execs().with_status(0)); }); + +test!(plugin_build_script_right_arch { + if disabled() { return } + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + build = "build.rs" + + [lib] + name = "foo" + plugin = true + "#) + .file("build.rs", "fn main() {}") + .file("src/lib.rs", ""); + + assert_that(p.cargo_process("build").arg("-v").arg("--target").arg(alternate()), + execs().with_status(0) + .with_stdout(format!("\ +{compiling} foo v0.0.1 ([..]) +{running} `rustc build.rs [..]` +{running} `[..]build-script-build[..]` +{running} `rustc src[..]lib.rs [..]` +", compiling = COMPILING, running = RUNNING))); +}); -- 2.30.2